package org.youchan.util;

import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

public class ImageCache {
	private Map<String, Image> mCache;
	
	public ImageCache() {
		mCache = new HashMap<String,Image>();
	}
	
	public Image getImage(String url) throws IOException {
		if (mCache.containsKey(url)) {
			return mCache.get(url);
		} else {
			Image image = ImageIO.read(new URL(url));
			mCache.put(url, image);
			return image;
		}
	}
	
	public void removeImage(URL url) {
		mCache.remove(url);
	}
	
	public Image reloadImage(String url) throws IOException {
		Image image = ImageIO.read(new URL(url));
		mCache.put(url, image);
		return image;
	}
}
